<?php
global $m, $tab, $a;
require_once($AppUI->getModuleClass('projects'));
require_once($AppUI->getMasterDataClass('mdtype'));
require_once($AppUI->getMasterDataClass('mdstatus'));

$perm = &$AppUI->acl();
if (!$perm->userHasRole($AppUI->user_id, 'Accounting')
&& !$perm->userHasRole($AppUI->user_id, 'Marketing')
&& !$perm->userHasRole($AppUI->user_id, 'FFIA_Management')
&& !$perm->userHasRole($AppUI->user_id, 'FFIA_Staff')
&& !$perm->userHasRole($AppUI->user_id, 'General_Management')) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect("m=home&tab=0");
	exit;
}

header('Content-Type: text/xml');



$xml = new XmlWriter();
$xml->openMemory();
$xml->startDocument('1.0', 'UTF-8');
$xml->startElement('project');
$project_id = intval( dPgetParam( $_GET, 'project_id', 0 ) );
$project_iso_code = dPgetParam( $_GET, 'project_iso_code', '' );
$pro = new CProject();
$result = null;
if ($project_id) {
	$result = $pro->load($project_id);
} else {
	$result = $pro->loadByCode($project_iso_code);
}
if ($result == null || $result == false) {
	$xml->writeElement('success', 'false');
	$xml->writeElement('project_id', $project_id);
	$xml->writeElement('project_iso_code', $project_iso_code);
} else {
	$k = 'sa_expense_amount';
	$pro->$k = $pro->getExpenseAmount($pro->project_id); 
	$k = 'sa_sales_amount';
	$pro->$k = $pro->getSalesAmount($pro->project_id);
	$k = 'has_task_or_task_log'; 
	$pro->$k = $pro->hasTaskOrTaskLog($pro->project_id) ? 'true' : 'false';
	$hash = get_object_vars($pro);
	foreach ($hash as $k => $v) {
		if( is_array($v) or is_object($v) or $k[0] == '_' ) // internal or NA field
			continue;
	
		if ($v == '') {
			$val = 'NULL';
		} else {
			$val = "'" . db_escape(htmlspecialchars( $v )) . "'";
		}
	    $xml->writeElement($k, addslashes(htmlentities($v)));
	}	
}

$xml->endElement();
echo $xml->outputMemory(true);
?>